package cndw.core.coder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import cndw.core.dispatch.Response;

/**
 * 用JAVA自带的序列化功能编码
 * @author zuojie.x
 */
public class JavaSerialEncoder implements Encoder {

	@Override
	public byte[] encode(Response response) {
		ByteArrayOutputStream baos = null;
		ObjectOutputStream oos = null;
		byte[] bytes = null;
		try {
			baos = new ByteArrayOutputStream(128);
			oos = new ObjectOutputStream(baos);
			oos.writeObject(response);
			bytes = baos.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (oos != null) oos.close();
				if (baos != null) baos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return bytes;
	}

}
